<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/16
 * Time: 23:45
 * 描述：大淘客数据接口
 */

namespace App\Http\Controllers;
use App\Models\DtkModel;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DtkAPIController extends Controller
{
    public $apiUrl = "http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=wk5awl51op&v=2&page=";


    public function getApiUrl($page=1)
    {
        return $this->apiUrl.$page;
    }
    public function getApiData()
    {//test
        $http = new \Http\Httplib($this->getApiUrl());
        $http->send();
        $webHtml = $http->response();
        $obj = json_decode($webHtml);
        if($obj=='' || $obj==null)
        {
            //错误后，跳转或传递异常
            exit("没获取到数据");
        }
        $res = $obj->result;
        $updateArr = array();
        $insertArr = array();

        $dtkModel = new DtkModel();

        foreach($res as $k=>$v)
        {
            $item= (array)$v;
            $item['Ctime'] = date("Y-m-d H:i:s");

            if($dtkModel->isExist($item['ID']))
            {//存在更新
                $updateArr= array_push($updateArr,$item);
                dd($updateArr);
                //exit;
            }else{//不存在添加
                $insertArr= array_push($insertArr,$item);
                var_dump($insertArr);
                //exit;
            }
            //var_dump($item);
            $dtkModel = new \App\Models\DtkModel();
            $dtkModel->addOneData($item);
            exit;
        }
    }
    public function getData($page='1')
    {
        $http = new \Http\Httplib($this->getApiUrl($page));
        $http->send();
        $webHtml = $http->response();
        $obj = json_decode($webHtml);
        if($obj=='' || $obj==null)
        {
            //错误后，跳转或传递异常
            exit("没获取到数据");
        }
        $res = $obj->result;
        $result = array();

        foreach($res as $k=>$v)
        {
            $item= (array)$v;
            $result[] = $item;
        }
        return $result;

    }
    public function syn()
    {
        return view("/admins/dtk/syn");
    }
    public function synDataBase($data)
    {
        $updateArr = array();
        $insertArr = array();
        $dtkModel = new \App\Models\DtkModel();
        //dd($data);

        foreach($data as $k=>$v)
        {
            $v['Ctime'] = date("Y-m-d H:i:s");
            $bool = $dtkModel->isExist($v['ID']);
            if($bool)
            {//存在更新
                $updateArr[]= $v;
            }else{//不存在添加
                $insertArr[]= $v;
            }
        }
        $ins = false;
        $up = false;
        if(!empty($insertArr))
        {
            $ins = $dtkModel->addData($insertArr);
        }
        if(!empty($updateArr))
        {
            foreach ($updateArr as $k=>$v)
            {
                $up = $dtkModel->updateData($v);
            }

        }
        if($ins && $up)
        {
            return true;
        }
        return false;
    }


    /*以session计算页数
     */
    public function synData(Request $request)
    {
        $totle = $request->session()->get('dtktotle');
        $curPage = $request->session()->pull('dtkpages');//从Session 中取出并删除数据
        $totle = $totle[0];
        $curPage = $curPage[0];
        //数组表示的含义，syn是否循环同步，seelp为停留几秒后跳转
        $msg = array();//['syn'=>true,'totle'=>$totle,'curpage'=>$curPage,'seelp'=>'3']

        //!-数据更新操作-start
        $dtkModel = new DtkModel();
        //获取数据
        $res = $this->getData($curPage);
        //进行入库
        $bool = $this->synDataBase($res);
        var_dump($bool);
        //!--end

        if($curPage<$totle)
        {
            $curPage = $curPage+1;
            $request->session()->push('dtkpages', $curPage);
            $msg = ['syn'=>true,'totle'=>$totle,'curpage'=>$curPage,'seelp'=>'10'];
            return view("/admins/dtk/syndata",$msg);
        }
        else{
            $msg = ['syn'=>false,'totle'=>$totle,'curpage'=>$curPage,'seelp'=>null];
            //exit('更新完毕');
            return view("/admins/dtk/syndata",$msg);
        }

    }

    /*初始化session，
     */
    public function startSyn(Request $request)
    {
        //计算有多少页，然后存入session,进行synData数据同步
        $pages = $this->getPages();
        if($pages)
        {
            $request->session()->push('dtktotle', $pages);
            $request->session()->push('dtkpages', 1);
            //$value = $request->session()->pull('key', 'default');//从Session 中取出并删除数据
            var_dump($request->session()->get('dtktotle'));
            return redirect("/admins/dtk/syndata");
        }
    }
    public function getPages()
    {
        $http = new \Http\Httplib($this->getApiUrl());
        $http->send();
        $webHtml = $http->response();
        $obj = json_decode($webHtml);
        if($obj)
        {
            $totleNum = $obj->data->total_num;
            return (int)ceil($totleNum/50);
        }
        else
        {
            return false;
        }
    }
    public function getList()
    {
        $dtkModel = new \App\Models\DtkModel();
        $res = $dtkModel->listData();
        //dd($res);
        return view("/admins/dtk/getlist",['res'=>$res]);


    }
    public function delDtkData(Request $request)
    {
        $day = 2;
        $day = $request->input('day');
        $dtkModel = new \App\Models\DtkModel();
        $res = $dtkModel->delData($day);
        echo "数据维护完毕，共删除了".$res."条记录";
       //return view("/admins/dtk/getlist");
    }
    public function del()
    {
        return view("/admins/dtk/del");
    }


}